package com.huilian.test.pdf;

import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.BaseFont;
import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.itext.extension.font.IFontProvider;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.opensagres.xdocreport.utils.StringUtils;

/**
 * @author Rocca
 *
 */
public class WordPdfUtils {

	protected static final Logger logger = LoggerFactory.getLogger(WordPdfUtils.class);
	
    public static void main(String[] args) throws Exception{
		String filepath = "F:\\待办\\WordToPdf\\个人借款合同.docx";
		String outpath = "F:\\待办\\WordToPdf\\22.pdf";
        
        InputStream source = new FileInputStream(filepath);
        OutputStream target = new FileOutputStream(outpath);
        Map<String, String> params = new HashMap<String, String>();
		params.put("name","张宁");


        //PdfOptions pdfOptions = PdfOptions.create();

		PdfOptions options = PdfOptions.create().fontEncoding(StandardCharsets.UTF_8.toString());
		//Options options = Options.getTo(ConverterTypeTo.PDF);
		//fr.opensagres.odfdom.converter.pdf.PdfOptions pdfOptions = fr.opensagres.odfdom.converter.pdf.PdfOptions.create();
		//pdfOptions.fontProvider(fontProvide);
		//options.subOptions(pdfOptions);

        
        wordConverterToPdf(source, target, options, params);
    }

	/**
     * 将word文档， 转换成pdf, 中间替换掉变量
     * @param source 源为word文档， 必须为docx文档
     * @param target 目标输出
     * @param params 需要替换的变量
     * @throws Exception
     */
    public static void wordConverterToPdf(InputStream source,
			OutputStream target, Map<String, String> params) throws Exception {
		wordConverterToPdf(source, target, null, params);	
	}

	/**
     * 将word文档， 转换成pdf, 中间替换掉变量
     * @param source 源为word文档， 必须为docx文档
     * @param target 目标输出
     * @param params 需要替换的变量
     * @param options PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他
     * @throws Exception
     */
    public static void wordConverterToPdf(InputStream source, OutputStream target, 
    		PdfOptions options,
    		Map<String, String> params) throws Exception {
    	 XWPFDocument doc = new XWPFDocument(source);
         paragraphReplace(doc.getParagraphs(), params);
         for (XWPFTable table : doc.getTables()) {
 			for (XWPFTableRow row : table.getRows()) {
 				for (XWPFTableCell cell : row.getTableCells()) {
 					paragraphReplace(cell.getParagraphs(), params);
 				}
 			}
 		}
 		PdfConverter.getInstance().convert(doc, target, options);
    }
    
    /** 替换段落中内容 */
	private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {
		if (MapUtils.isNotEmpty(params)) {
			for (XWPFParagraph p : paragraphs){
				for (XWPFRun r : p.getRuns()){
					String content = r.getText(r.getTextPosition());
					logger.info(content);
					if(StringUtils.isNotEmpty(content) && params.containsKey(content)) {
						r.setText(params.get(content), 0);
					}
				}
			}
		}
	}
	
}